// pages/Player/Player.js
const app = getApp()
import http from '../../utils/request.js'
import api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    current: 0,
    playersTotalAll: null,
    playersTotal100: null,
    playersTotal20: null,
    recentMatches: null,
    playersPeers: null,
    playersEnemies: null,
    maxs: null,
    player: null,
    tabs: ['综合统计', '数据统计', '队友对手'],
    totals: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let {
      id,
    } = options
    this.setData({
      id,
    })
    wx.showLoading({
      title: '加载中...',
    })
    http.get(api.players + id)
      .then(result => {
        wx.hideLoading()
        this.setData({
          player: result.data.data,
        })
      })
    this.callComponent()
  },
  tabSelect(e) {
    this.setData({
      current: e.currentTarget.dataset.id
    })
  },
  change(e) {
    this.setData({
      current: e.detail.current
    })
    this.callComponent()
  },
  callComponent() {
    let current = this.data.current
    if (current == 0) {
      this.getTotals();
    } else if (current == 1) {
      this.getDatas();
    } else if (current == 2) {
      this.getPeers();
    }
  },
  getTotals() {
    if (this.data.totals === null) {
      http.get(api.playerTotals, {
          id: this.data.id
        })
        .then(result => {
          this.setData({
            totals: result.data.data
          })
        })
    }
  },
  getDatas() {
    if (this.data.maxs != null) {
      return;
    }
    http.get(api.playerMaxs, {
        id: this.data.id
      })
      .then(result => {
        let maxs = result.data.data;
        for (let key in maxs) {
          let max = maxs[key];
          max.endTime = new Date(max.endTime).getTime() / 1000
        }
        this.setData({
          maxs
        })
      })
  },
  getPeers() {
    if (this.data.playersPeers == null) {
      http.get(api.playerPeers, {
          id: this.data.id
        })
        .then(result => {
          this.setData({
            playersPeers: result.data.data.records
          })
        })
    }
    if (this.data.playersEnemies == null) {
      http.get(api.playerEnemies, {
          id: this.data.id
        })
        .then(result => {
          this.setData({
            playersEnemies: result.data.data.records,
          })
        })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
})